package cn.edu.cug.cs.gtl.apis.ui.projecttree;

import javax.swing.event.TreeModelEvent;
import javax.swing.tree.DefaultMutableTreeNode;

import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.tree.Tree;
import cn.edu.cug.cs.gtl.apis.ui.tree.TreeModelListener;
import cn.edu.cug.cs.gtl.apis.ui.tree.TreeNodeData;
import cn.edu.cug.cs.gtl.protos.Layer;
import cn.edu.cug.cs.gtl.protos.Map;
import cn.edu.cug.cs.gtl.protos.Project;
import cn.edu.cug.cs.gtl.apis.render.nodes.Node;

public class ProjectTreeModelListener extends TreeModelListener {

    public ProjectTreeModelListener(Tree tree, AbstractApplication theApp) {
        super(tree, theApp);
    }

    @Override
    public void treeNodesChanged(TreeModelEvent e) {
        Object [] children = e.getChildren();
        if(children!=null && children.length>0){
            DefaultMutableTreeNode node =(DefaultMutableTreeNode) (children[0]);
            TreeNodeData projectTreeNodeData = (TreeNodeData)node.getUserObject();
            Object refObject = projectTreeNodeData.getRefObject();
            boolean visible = projectTreeNodeData.isChecked();
            Node sceneGraph = theApp.getMainFrame().getCurrentRender().getSceneGraph();
            if(refObject instanceof Project) {
                theApp.getMainFrame().getCurrentRender().setVisible((Project) refObject,visible);
            }
            else if(refObject instanceof Map) {
                theApp.getMainFrame().getCurrentRender().setVisible((Map) refObject,visible);
            }
            else if(refObject instanceof Layer) {
                theApp.getMainFrame().getCurrentRender().setVisible((Layer) refObject,visible);
            }
            else{

            }
            theApp.getMainFrame().getCurrentRender().getViewer().viewAll();
            theApp.getMainFrame().getCurrentRender().getViewer().getRenderArea().invalidate();
        }
    }

    @Override
    public void treeNodesInserted(TreeModelEvent e) {

    }

    @Override
    public void treeNodesRemoved(TreeModelEvent e) {

    }

    @Override
    public void treeStructureChanged(TreeModelEvent e) {

    }
}
